<?php

namespace Home\Controller;

use Think\Controller;
use Think\IFilter;
use Think\IReq;
use Think\ITime;
use Think\JSON;
use Home\Model\Tool;
use Home\Model\Game;
class IndexController extends Controller {

    public $layout = 'default';
    public function index() {
        $code = IFilter::act(IReq::get("code"));
        if(!$code){
            $this->redirect('index/mes');
        }
        $codeDB = M('code as c');
        $field = 'c.id, c.good_id, c.code,c.area as area, c.status, c.num, c.add,c.ip, g.sell_price, g.img, g.seller_id, g.name';
        $join = 'left join sm_goods as g on c.good_id = g.id';
        $code = $codeDB->join($join)->field($field)->where(array('code' => $code))->find();
        $area = substr($code['area'], 0,9);
        //$where = "area_name like '".$area."%'";
        $areaid = (int)M('areas')->where(array('area_name' => $area))->getField('area_id');
        if(!$code){
            $this->redirect('index/mes');
        }
        $seller = M('seller_set')->where(array('seller_id' => $code['seller_id']))->find();
        $menu = M('menu')->where(array('seller_id' => $code['seller_id'],'showInhome' => "1"))->find();
        $menu['dataset'] = JSON::decode($menu['dataset']);
        foreach($menu['dataset'] as $k=>$v){
            if (!preg_match('/(http:\/\/)|(https:\/\/)/i', $v['link'])) {
                $menu['dataset'][$k]['link'] = "http://".$v['link'];
                if(!empty($v['subNav'])){
                    foreach($v['subNav'] as $key=>$vv){
                        if(!preg_match('/(http:\/\/)|(https:\/\/)/i', $vv['link'])){
                            $menu['dataset'][$key]['subNav'][$key]['link'] = "http://".$vv['link'];
                        }
                    }
                }
            }
        } 
        $user_IP = Tool::getIP();
        $code['ip'] = $user_IP;
        //dump(M('seller_set')->getLastSql());
        //dump($seller);die;
        if ($code['id']) {
            //查询是否参加过活动
            $doPrizeDB = M('do_prize');
            $isDoPrize = $doPrizeDB->where("codeid=" . $code['id'])->find();
            $isPrize = empty($isDoPrize) ? 1 : 2;
            $this->assign("isPrize", $isPrize);
        }
        
        //促销活动
        $game =  Game::is_open($code);
        $this->assign('menu',$menu);
        $this->assign('areaid',$areaid);
        $this->assign('game',$game);
        $this->assign('code',$code);
        $this->assign('seller',$seller);
        $this->assign('layout',$layout);
        $this->display();
    }
    public function mes(){
        $user_IP = Tool::getIP();
        $this->assign("ip",$user_IP);
        $this->display();
    }
    public function upAdd(){
        $id = IFilter::act(IReq::get("id"));
        $add = IFilter::act(IReq::get("add"));
        //dump($id);
        //dump($add);
        $where = array(
            'id' => $id
        );
        $codeDB = M('code');
        $code = $codeDB->where($where)->find();
        //dump($codeDB->getLastSql());
        //dump($code);die;
        $user_IP = Tool::getIP();
        if($code['status'] == '1' && $code['num'] == 0){
            $data = array(
                'add' => $add,
                'num' => 1,
                'status' => '2',
                'ip' => $user_IP,
                'time' => ITime::getNow('Y-m-d H:i:s')
            );
        }
            
        $system = Tool::getSystem();
        $Broswer = Tool::getClientBroswer();
        $netType = Tool::getNetType();
        
        $data['num'] = $code['num'] + 1;
        $log = array(
            'code_id' => $code['id'],
            'IP' => $user_IP,
            'time' => ITime::getDateTime(),
            'add' => $add,
            'num' => $data['num'],
            'cookie' => $_SERVER['HTTP_COOKIE'],
            'system' => $system['system'],
            'model' => $system['model'],
            'broswer' => $Broswer,
            'netType' => $netType
        );
        //添加二维码扫描日志
        M('code_log')->add($log);
        
        $codeDB->where($where)->save($data);
    }

    public function nav(){
        $id = IFilter::act(IReq::get("id")) ? IFilter::act(IReq::get("id")) : 1;
        $seller_id = IFilter::act(IReq::get("seller_id"));
        if(!$seller_id){
            $this->redirect('index/mes');
        }
        $where = array(
            'seller_id' => $seller_id,
        );
        $con = M('seller_set')->where($where)->find();
        switch ($id) {
            case 1:
                $mes = array(
                    'title' => $con['nav1'],
                    'info' => $con['nav1_value']
                );
                break;
            case 2:
                $mes = array(
                    'title' => $con['nav2'],
                    'info' => $con['nav2_value']
                );
                break;
            case 3:
                $mes = array(
                    'title' => $con['nav3'],
                    'info' => $con['nav3_value']
                );
                break;
        }
        $this->assign('con',$con);
        $this->assign('mes',$mes);
        $this->display();
    }
    
    public function aa(){
        dump($_SERVER);
        $aa = Tool::getSystem('Mozilla/5.0 (Linux; Android 4.4.4; vivo X5S L Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/37.0.0.0 Mobile MQQBrowser/6.8 TBS/036824 Safari/537.36 MicroMessenger/6.3.27.880 NetType/WIFI Language/zh_CN');
        dump($aa);die;
        $this->display('index');
    }
}